package com.fjh.controller;

import com.fjh.pojo.Student;
import com.fjh.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping
    public ResponseEntity<Student> addStudent(@RequestBody Student student) {
        Student savedStudent = studentService.addStudent(student);
        return ResponseEntity.status(HttpStatus.CREATED).body(savedStudent);
    }

    @PostMapping("/batch")
    public List<Student> addStudents(@RequestBody List<Student> students) {
        return studentService.addStudents(students);
    }

    @GetMapping("/{studentId}")
    public Optional<Student> getStudentById(@PathVariable Long studentId) {
        return studentService.findStudentById(studentId);
    }

    @GetMapping("/status/{status}")
    public List<Student> getStudentsByStatus(@PathVariable Integer status) {
        return studentService.findStudentsByStatus(status);
    }

    @GetMapping
    public List<Student> getAllStudents() {
        return studentService.findAllStudents();
    }

    @PutMapping
    public Student updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }

    @DeleteMapping("/{studentId}")
    public void deleteStudent(@PathVariable Long studentId) {
        studentService.deleteStudent(studentId);
    }
    // 审核单个学员
    @PostMapping("/{studentId}/approve")
    public ResponseEntity<Student> approveStudent(@PathVariable Long studentId) {
        Optional<Student> optionalStudent = studentService.findStudentById(studentId);

        if (optionalStudent.isPresent()) {
            Student student = optionalStudent.get();
            student.setStatus(1); // 设置为已审核状态，具体根据你的业务逻辑调整

            // 保存更新后的学员信息
            Student updatedStudent = studentService.updateStudent(student);
            return ResponseEntity.ok(updatedStudent);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 批量审核学员
    @PostMapping("/batch/approve")
    public ResponseEntity<List<Student>> approveStudents(@RequestBody List<Long> studentIds) {
        List<Student> approvedStudents = new ArrayList<>();

        for (Long studentId : studentIds) {
            Optional<Student> optionalStudent = studentService.findStudentById(studentId);
            optionalStudent.ifPresent(student -> {
                student.setStatus(1); // 设置为已审核状态，具体根据你的业务逻辑调整
                studentService.updateStudent(student);
                approvedStudents.add(student);
            });
        }

        return ResponseEntity.ok().build();
    }

}
